package com.example.util;

import java.util.Random;

public class RandomNumber {

    public static int generateRandomNumber(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于 0");
        }

        Random random = new Random();
        int min = (int) Math.pow(10, length - 1); // 最小值，例如长度为 3 时，min = 100
        int max = (int) Math.pow(10, length) - 1; // 最大值，例如长度为 3 时，max = 999

        return min + random.nextInt(max - min + 1);
    }

    public static String generateRandomNumberStr(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10)); // 生成 0-9 的随机数
        }
        return sb.toString();
    }

}
